package com.bort.system.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bort.system.info.DeskInfo;
import com.bort.system.model.TDesk;
import com.bort.system.model.TGameLog;
import com.bort.system.service.TDeskService;
import com.bort.system.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/admin/desk")
public class AdminDeskController {

    @Resource
    TDeskService deskService;

    @RequestMapping("/list")
    public Result<Page<DeskInfo>> index(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rowSize", defaultValue = "12") Integer rowSize,
            @RequestParam(value = "deskType",required = false)Integer deskType,
            @RequestParam(value = "deskNo",required = false)Integer deskNo
    ) {
        Page<DeskInfo> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(rowSize);
        Map<String,Object> condition = new HashMap<>();
        condition.put("deskNo",deskNo);
        condition.put("deskType",deskType);
        deskService.getDeskList(pager, condition);
        Result<Page<DeskInfo>> result = new Result<>();
        result.setData(pager);
        return result;
    }

    @RequestMapping("/add")
    public Result<Page<DeskInfo>> addDesk(@RequestBody TDesk desk){
        if(desk == null){
            return Result.error("数据有误");
        }
        int in = deskService.addDesk(desk);
        return Result.success("新增成功");
    }

    @RequestMapping("/update")
    public Result<Page<DeskInfo>> updateDesk(@RequestBody TDesk desk){
        if(desk == null){
            return Result.error("数据有误");
        }
        int in = deskService.updateDesk(desk);
        return Result.success("更新成功");
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(@RequestParam Integer id) {
        int del = deskService.deleteDesk(id);
        return Result.success();
    }

    @RequestMapping(value = "/gameLog",method = RequestMethod.GET)
    public Result<Page<TGameLog>> gameLog(
            @RequestParam(value = "current", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "12") int rowSize,
            @RequestParam(value = "deskId",defaultValue = "0")int deskId,
            @RequestParam(value = "timeBegin",required = false) String timeBegin,
            @RequestParam(value = "timeEnd",required = false) String timeEnd
    ) {
        if(deskId == 0){
            return Result.error("参数有误");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("deskId",deskId);
        map.put("timeBegin",timeBegin);
        map.put("timeEnd",timeEnd);
        Page<TGameLog> pager = deskService.getGameLogList(page, rowSize,map);
        return Result.success(pager);
    }

    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public Result<Map<String,Object>> gameLogCount(
            @RequestParam(value = "deskId",defaultValue = "0")int deskId,
            @RequestParam(value = "timeBegin",required = false) String timeBegin,
            @RequestParam(value = "timeEnd",required = false) String timeEnd
    ) {
        Map<String,Object> map = new HashMap<>();
        map.put("deskId",deskId);
        map.put("timeBegin",timeBegin);
        map.put("timeEnd",timeEnd);
        return deskService.getDataCount(map);
    }

}
